/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DocumentoPersona.java
 *
 * Created on 20-feb-2011, 11:45:05
 */
package com.catosist.run.application.persona.documents;

import com.catosist.run.application.Util.MiModelo;
import com.catosist.run.business.gestorDocumentosPersona.GestorDocumentosPersona;
import com.catosist.run.services.DocpersDTO;
import java.util.Iterator;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.table.DefaultTableModel;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 *
 * @author Bicho
 */
public class DocumentoPersona extends javax.swing.JFrame {

    /** Creates new form DocumentoPersona */
    public DocumentoPersona() {
        initComponents();
    }

    private DocumentoPersona(boolean b) {
        initComponents();
        ApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext.xml");
        gestorDocumentosPersona = (GestorDocumentosPersona) ctx.getBean("gestorDocumentosPersona");
        personaId = 12;
        refresh();
    }

    public DocumentoPersona(int i) {
        initComponents();
        ApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext.xml");
        gestorDocumentosPersona = (GestorDocumentosPersona) ctx.getBean("gestorDocumentosPersona");
        personaId = i;
        refresh();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        btnNuevo = new javax.swing.JButton();
        btnEditar = new javax.swing.JButton();
        btnEliminar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setName("Form"); // NOI18N

        jPanel1.setName("jPanel1"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jTable1.setName("jTable1"); // NOI18N
        jScrollPane1.setViewportView(jTable1);

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(com.catosist.run.application.desktopapplication.DesktopApplication.class).getContext().getResourceMap(DocumentoPersona.class);
        btnNuevo.setText(resourceMap.getString("btnNuevo.text")); // NOI18N
        btnNuevo.setName("btnNuevo"); // NOI18N
        btnNuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNuevoActionPerformed(evt);
            }
        });

        btnEditar.setText(resourceMap.getString("btnEditar.text")); // NOI18N
        btnEditar.setName("btnEditar"); // NOI18N
        btnEditar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditarActionPerformed(evt);
            }
        });

        btnEliminar.setText(resourceMap.getString("btnEliminar.text")); // NOI18N
        btnEliminar.setName("btnEliminar"); // NOI18N
        btnEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEliminarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 699, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                        .addComponent(btnNuevo)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnEditar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnEliminar)))
                .addGap(344, 344, 344))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnNuevo)
                    .addComponent(btnEditar)
                    .addComponent(btnEliminar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 304, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 503, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEliminarActionPerformed
        try {
            int i = Integer.parseInt(this.jTable1.getValueAt(this.jTable1.getSelectedRow(), 0).toString());
            if (i == 0) {
                throw new Exception();
            }
            int result = JOptionPane.showConfirmDialog(this, "Realmente desea eliminar el registro?", "Eliminar Gasto", JOptionPane.YES_NO_OPTION);
            if (result == JOptionPane.YES_OPTION) {
                gestorDocumentosPersona.deleteById(i, personaId);
                refresh();
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "Debe seleccionar un documento existente", "Advertencia", JOptionPane.WARNING_MESSAGE);
        }
    }//GEN-LAST:event_btnEliminarActionPerformed

    private void btnNuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNuevoActionPerformed
        DialogDocumentoPersona dialogDocumentoPersona = new DialogDocumentoPersona(this, true, personaId, 0, gestorDocumentosPersona);
        dialogDocumentoPersona.setVisible(true);
        refresh();
    }//GEN-LAST:event_btnNuevoActionPerformed

    private void btnEditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditarActionPerformed
        try {
            int i = Integer.parseInt(this.jTable1.getValueAt(this.jTable1.getSelectedRow(), 0).toString());
            if (i == 0) {
                throw new Exception();
            }
            DialogDocumentoPersona dialogDocumentoPersona = new DialogDocumentoPersona(this, true, personaId, i, gestorDocumentosPersona);
            dialogDocumentoPersona.setLocationRelativeTo(this);
            dialogDocumentoPersona.setVisible(true);
            refresh();
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "Debe seleccionar un documento existente", "Advertencia", JOptionPane.WARNING_MESSAGE);
        }
    }//GEN-LAST:event_btnEditarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new DocumentoPersona(true).setVisible(true);
            }
        });
    }

    private void refresh() {
        DefaultTableModel model = new MiModelo();
        model.addColumn("id");
        model.addColumn("Nombre");
        model.addColumn("Numero");
        model.addColumn("F. Inicio");
        model.addColumn("F. Fin");
        model.addColumn("Agente");

        List<DocpersDTO> docpersDTOs = gestorDocumentosPersona.getByPersonaId(personaId);
        for (Iterator<DocpersDTO> it = docpersDTOs.iterator(); it.hasNext();) {
            DocpersDTO docpersDTO = it.next();
            model.addRow(docpersDTO.toArray());
        }
        this.jTable1.setModel(model);
    }

    public JPanel getPanel() {
        return this.jPanel1;
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnEditar;
    private javax.swing.JButton btnEliminar;
    private javax.swing.JButton btnNuevo;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables
    private GestorDocumentosPersona gestorDocumentosPersona;
    private int personaId;
}
